package org.jsola.hr.constant;

import org.jsola.core.validator.IConst;
import org.jsola.json.JsonKit;

import java.util.HashMap;
import java.util.Map;

/**
 * 社保尾数处理类型
 *
 * @author zhr
 */
public enum MantissaProcessingType implements IConst {

    /**
     * 不处理
     */
    NOT_PROCESSING(0,"不处理"),
    /**
     * 四舍五入到元
     */
    ROUND_HALF_UP_Y(1,"四舍五入到元"),
    /**
     * 四舍五入到角
     */
    ROUND_HALF_UP_J(2,"四舍五入到角"),
    /**
     * 四舍五入到分
     */
    ROUND_HALF_UP_F(3,"四舍五入到分"),
    /**
     * 四舍五入到厘
     */
    ROUND_HALF_UP_L(4,"四舍五入到厘"),
    /**
     * 四舍五入到小数点后四位
     */
    ROUND_HALF_UP_S(5,"四舍五入到小数点后四位"),
    /**
     * 见厘进分
     */
    CARRY_F(6,"见厘进分"),
    /**
     * 见分进角
     */
    CARRY_J(7,"见分进角"),
    /**
     * 见角进元
     */
    CARRY_Y(8,"见角进元"),
    /**
     * 见角舍去
     */
    REJECTION_J(9,"见角舍去"),
    /**
     * 见分舍去
     */
    REJECTION_F(10,"见分舍去"),
    /**
     * 见厘舍去
     */
    REJECTION_L(11,"见厘舍去");

    private final Integer value;
    private final String desc;
    /**
     * 枚举值罗列，给swagger接口文档展示用
     */
    public static final String VALUES_STR = "0,1,2,3,4,5,6,7,8,9,10,11";

     /**
     * 缓存所有value
     */
    private static final Map<Integer, MantissaProcessingType> LOOKUP = new HashMap<>();

    static {
        for (MantissaProcessingType e : MantissaProcessingType.values()) {
            LOOKUP.put(e.value, e);
        }
    }


    MantissaProcessingType(Integer value, String desc) {
        this.value = value;
        this.desc = desc;
    }

    /**
     * 根据value查找MantissaProcessingType
     * @param value value
     * @return MantissaProcessingType
     */
    public static MantissaProcessingType find(Integer value) {
        return LOOKUP.get(value);
    }

    /**
     * 根据desc查找MantissaProcessingType
     * @param desc desc
     * @return MantissaProcessingType
     */
    public static MantissaProcessingType findByDesc(String desc){
        for (MantissaProcessingType e : MantissaProcessingType.values()) {
            if(e.getDesc().equals(desc)){
                return e;
            }
        }
        return null;
    }

    /**
     * 所有枚举转json
     * @return json
     */
    public static String getAllToJson() {
        Map<String,Object> map = new HashMap<>();
        for (MantissaProcessingType e : MantissaProcessingType.values()) {
            map.put("value", e.value);
            map.put("desc", e.desc);
        }
        return JsonKit.toJSONString(map);
    }

    @Override
    public boolean check(Object obj) {
        Integer value = (Integer) obj;
        return find(value) != null;
    }

    public Integer getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }
}
